using MediatR;
using Medical.Api.Read.Application.Command.BasicManagement.DeviceDictionaryCommand;
using Medical.Api.Read.Application.Command.Wms.WmsStorageCommand;
using Medical.Api.Read.Application.Command.Wms.WmsStoresCommand;
using Medical.Api.Read.Application.Command.Wms.WmsOutCauseCommand;
using Medical.Api.Read.Dto.BasicManagement.DeviceDictionaryDto;
using Medical.Api.Read.Dto.WmsDto.WmsStorageDto;
using Medical.Api.Read.Dto.WmsDto.WmsStoresDro;
using Medical.Api.Read.Service.WmsService;
using Medical.Domain.BasicManagement;
using Medical.Domain.Wms;
using Medical.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Medical.Api.Read.Dto.WmsDto.WmsOutDto;
using Medical.Api.Read.Dto.WmsDto.WmsLssueDto;
using Medical.Api.Read.Application.Command.Wms.WmsLssueCommand;
using Medical.Api.Read.Application.Command.Wms.WmsOutCauseCommand;

namespace Medical.Api.Read.Controllers
{
    /// <summary>
    /// WMS（仓储管理）相关接口，包含出库、存放、物品等查询。
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class WmsController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<WmsController> _logger;

        /// <summary>
        /// 构造函数，注入MediatR和日志
        /// </summary>
        public WmsController(IMediator mediator, ILogger<WmsController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        #region 入库
        /// <summary>
        /// 查询入库列表
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns>分页入库列表</returns>
        [HttpGet]
        public ApiResult<ApiPageIng<WmsStoragePageDto>> QueryEquipment([FromQuery] StorageQueryDto dto)
        {
            try
            {
                _logger.LogInformation("开始查询入库列表");
                var result = _mediator.Send(new QueryStorageCommand(dto)).Result;
                _logger.LogInformation("查询入库列表成功");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询入库列表失败");
                return new ApiResult<ApiPageIng<WmsStoragePageDto>>
                {
                    Code = ApiEnums.Error,
                    Msg = ex.Message
                };
            }
        }

        /// <summary>
        /// 获取入库详情
        /// </summary>
        /// <param name="id">入库主表Id</param>
        /// <returns>入库详情</returns>
        [HttpGet]
        public ApiResult<WmsStorageDetailDto> GetStorageById([FromQuery] int id)
        {
            try
            {
                _logger.LogInformation($"开始查询入库详情，Id: {id}");
                var result = _mediator.Send(new GetStorageByIdCommand(id)).Result;
                _logger.LogInformation($"查询入库详情成功，Id: {id}");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"查询入库详情失败，Id: {id}");
                return new ApiResult<WmsStorageDetailDto>
                {
                    Code = ApiEnums.Error,
                    Msg = ex.Message
                };
            }
        }


        #endregion


        #region 存放
        /// <summary>
        /// 获取指定货架的存放列表
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns>存放信息列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<WmsStoresPageDto>>> GetStoresList([FromQuery] QueryStoresDto dto)
        {
            try
            {
                _logger.LogInformation("开始查询存放列表");
                var result = await _mediator.Send(new GetStoresListCommand(dto));
                _logger.LogInformation("查询存放列表成功");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询存放列表失败");
                return new ApiResult<List<WmsStoresPageDto>>
                {
                    Code = ApiEnums.Error,
                    Msg = ex.Message
                };
            }
        }

        /// <summary>
        /// 获取已审批的入库主表列表
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<List<WmsStoragePageDto>>> GetApprovedStorageList()
        {
            var service = HttpContext.RequestServices.GetService(typeof(IWmsService)) as IWmsService;
            try
            {
                var list = await service.GetApprovedStorageListAsync();
                return new ApiResult<List<WmsStoragePageDto>>
                {
                    Code = ApiEnums.Success,
                    Data = list,
                    Msg = "获取成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<WmsStoragePageDto>>
                {
                    Code = ApiEnums.Error,
                    Data = null,
                    Msg = $"获取已审批入库列表异常: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 获取待存放物品列表
        /// </summary>
        /// <returns>待存放物品列表</returns>
        [HttpGet]
        public async Task<List<WaitStoreDto>> GetWaitStoresLisat()
        {
            return await _mediator.Send(new GetWaitStoresLisatCommand());
        }

        /// <summary>
        /// 获取物品详情
        /// </summary>
        [HttpGet]
        public async Task<MaterialDetailDto> GetMaterialById([FromQuery] int id, [FromQuery] int materialType)
        {
            return await _mediator.Send(new GetMaterialByIdCommand(id, materialType));
        }

        /// <summary>
        /// 获取货架详情
        /// </summary>
        /// <param name="id">货架ID</param>
        /// <returns>货架详情</returns>
        [HttpGet]
        public async Task<ApiResult<ShelvesDetailDto>> GetShelvesDetailById([FromQuery] int id)
        {
            return await _mediator.Send(new GetShelvesDetailByIdCommand(id));
        }

        //获取货架详情



        #endregion


        #region 出库

        /// <summary>
        /// 出库分页查询
        /// </summary>
        /// <param name="queryDto">查询参数</param>
        /// <returns>分页出库列表</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPageIng<WmsOutPageDto>>> GetOutCauseByPag([FromQuery] WmsOutQueryDto queryDto)
        {
            return await _mediator.Send(new GetOutCauseByPageCommand(queryDto));
        }

        /// <summary>
        /// 获取出库详情
        /// </summary>
        /// <param name="id">出库单id</param>
        /// <returns>出库详情及明细</returns>
        [HttpGet]
        public async Task<ApiResult<WmsOutDetailReadDto>> GetOutCauseDetailById([FromQuery] int id)
        {
            return await _mediator.Send(new GetOutCauseDetailByIdCommand(id));
        }
        #endregion


        #region 发放

        /// <summary>
        /// 发放表分页查询
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<ApiPageIng<WmsLssuePageDto>>> GetLssueByPage([FromQuery] WmsLssueQueryDto queryDto)
        {
            return await _mediator.Send(new GetLssueByPageCommand(queryDto));
        }

        /// <summary>
        /// <summary>
        /// 获取发放详情
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<WmsLssueDetailReadDto>> GetLssueDetailById([FromQuery] WmsLssueDto dto)
        {
            return await _mediator.Send(new GetLssueDetailByIdCommand(dto.Id));
        }


        /// <summary>
        /// 获取已审批的出库主表列表
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<List<WmsOutPageDto>>> GetApprovedOutList()
        {
            return await _mediator.Send(new GetApprovedOutListCommand());
        }

        #endregion


    }
}
